package gs.wicket.app.product;

import gs.wicket.app.GSApp;
import gs.wicket.app.session.GSSession;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.validation.validator.EmailAddressValidator;

public class AboutProduct extends ProductsPage{

	PageParameters parameters;
	
	  public AboutProduct(PageParameters parameters) {
		 
	    super();
	    this.parameters = parameters;
	    Product product = null;
	    if(parameters.containsKey("name")) {
	    	String name = parameters.getString("name");
	    	product = findProduct();
	    }
	    createComponents(product);
	   
	  }

	  
	  public AboutProduct(Product product) {
	    createComponents(product);
	  }
	  
	  
	  private void createComponents(final Product product) {
	    add(new Label("name", product.getName()));
	    add(new Image("image", product.getImageURL()));
	    add(new MultiLineLabel("longDescription", product.getLongDescription()).setEscapeModelStrings(false));
	    add(new Link("add", getModel()) {
	        
			private static final long serialVersionUID = 1L;
			{
				add(new Label("about.product.add", getLangProperty("about.product.add")));
			}
			@Override
	        public void onClick() {
	         // Product selected = (Product) getModelObject();
	          getCart().getProducts().add(product);
	        }
	      });
	    
	  
	    add(new MultiLineLabel("prevcomm", product.getComments()+product.getCurrentComments()).setEscapeModelStrings(false));
	    add(new Label("about.product.comments", getLangProperty("about.product.comments")));
	    add(new Label("about.product.add.comment", getLangProperty("about.product.add.comment")));
	    
	   
	    final List<String> comments = new ArrayList<String>();
	    final WebMarkupContainer parent = new WebMarkupContainer("comments");
	    parent.setOutputMarkupId(true);
	    add(parent);
	    parent.add(new ListView("list", comments) {
	      
			private static final long serialVersionUID = 1L;

		@Override
	      protected void populateItem(ListItem item) {
	        item.add(new Label("comment", item.getModel()));
	      }
	    });

	  
	    final TextField name = new TextField("name", new Model(""));
	    final TextField email = new TextField("email", new Model(""));
	    final TextArea editor = new TextArea("editor", new Model(" "));
	    final Button submit = new Button("submit"){
	    	
			private static final long serialVersionUID = 1L;
			{
				add(new AttributeModifier("value", new Model(getLangProperty("about.product.submit"))));
			}
				@Override
				public void onSubmit() {
				   GSApp.writeProductComment(product,name.getModelObjectAsString(),editor.getModelObjectAsString());
				   PageParameters pars = new PageParameters();
			       pars.add("name", product.getName());
			       setResponsePage(new AboutProduct(pars));
			        
			   }
		   } ;
	    
	    Form form = new Form("form");
	    
	    email.add(EmailAddressValidator.getInstance());
	    email.add(new SimpleAttributeModifier("maxlength", "32"));
	    editor.setOutputMarkupId(true);
	    
	    name.setRequired(true);
	    email.setRequired(true);
	    editor.setRequired(true);
	    
	    form.add(name);
	    form.add(email);
	    form.add(editor);
	    form.add(submit);
	    form.add(new Label("about.product.name", getLangProperty("about.product.name")));
	    form.add(new Label("about.product.mail", getLangProperty("about.product.mail")));
	    	    
	    parent.add(form);
	 	    
	  }
	  
	  private Product findProduct(){
		  List<Product> products = GSApp.getProducts(((GSSession) getSession()).getLanguage());
		  Product result = null;
		  for (Product product: products)
		  {
			if (product.getName().equals(parameters.getString("name"))) 
				result = product;
		  }
		  return result;
		
	  }
}
